package com.piece.core.mongo.context;

import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.util.math.SequenceUtil;
import com.piece.core.framework.util.object.BeanUtil;
import com.piece.core.framework.util.string.JsonUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.log.client.BeanPostLogService;
import com.piece.core.log.client.dto.BeanPostLogDTO;
import com.piece.core.log.client.dto.BeanPostLogInfoDTO;
import com.piece.core.mongo.entity.MongoBeanPostLog;
import com.piece.core.mongo.service.MongoBeanPostLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据库变更日志实现类-mongo
 */
@Slf4j
@Component("bean_post_log_service_mongo")
@ConditionalOnProperty(name = "spring.log.type", havingValue = "mongo")
public class BeanPostLogServiceMongoImpl implements BeanPostLogService {

    @Resource
    private MongoBeanPostLogService mongoBeanPostLogService;

    @Override
    public BeanPostLogDTO save(BeanPostLogDTO logDTO) {
        try {
            MongoBeanPostLog beanPostLog = convertToDb(logDTO);
            beanPostLog.setLogId(SequenceUtil.uuid());
            beanPostLog = mongoBeanPostLogService.insert(beanPostLog);
            logDTO.setLogId(beanPostLog.getLogId());
            return logDTO;
        } catch (Exception e) {
            log.error("插入数据库变更日志异常：{}", e.getMessage());
        }

        return null;
    }

    @Override
    public void delete(String logId) {
        mongoBeanPostLogService.delete(logId);
    }

    @Override
    public List<BeanPostLogDTO> findByCondition(Map<String, Object> param) {
        QueryTable queryTable = new QueryTable();
        queryTable.setParams(param);
        List<MongoBeanPostLog> logs = mongoBeanPostLogService.findList(queryTable);
        return logs.stream().map(beanPostLog -> {
            BeanPostLogDTO logDTO = null;
            try {
                logDTO = convertToLog(beanPostLog);
            } catch (Exception e) {
                log.error("转换日志对象异常：{}", e.getMessage());
            } finally {
                return logDTO;
            }
        }).collect(Collectors.toList());
    }

    private MongoBeanPostLog convertToDb(BeanPostLogDTO logDTO) {
        MongoBeanPostLog beanPostLog = new MongoBeanPostLog();
        BeanUtil.copyProperties(logDTO, beanPostLog);
        List<BeanPostLogInfoDTO> infos = logDTO.getInfos();
        if (null != infos) {
            beanPostLog.setDetails(JsonUtil.toJson(infos));
        }

        return beanPostLog;
    }

    private BeanPostLogDTO convertToLog(MongoBeanPostLog beanPostLog) {
        BeanPostLogDTO logDTO = new BeanPostLogDTO();
        BeanUtil.copyProperties(beanPostLog, logDTO);
        if (StringUtil.isNotEmpty(beanPostLog.getDetails())) {
            logDTO.setInfos(JsonUtil.toBean(beanPostLog.getDetails(), List.class));
        }

        return logDTO;
    }
}
